package com.jxw.launcher;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.view.Window;
import com.jht.engine.platsign.PlatSignatureWrapper;
import com.jht.engine.platsign.SignUtil;
import com.jxw.constant.Constant;
import com.jxw.engine.platsign.MainActivity;
import com.jxw.gk.SPUtils;
import com.jxw.launcher.util.Logger;
import com.jxw.launcher.util.ReceiverUtil;
import com.jxw.launcher.util.UserData;
import com.jxw.launcher.util.Util;
import com.jxw.launcher.view.DialogUpdate;
import com.jxw.studyrecord.StudyRecordMng;
import com.jxw.utils.SystemPropertiesProxy;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.SortedMap;
import java.util.TreeMap;
import net.lingala.zip4j.util.InternalZipConstants;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import tv.danmaku.ijk.media.player.IjkMediaMeta;

/* loaded from: classes.dex */
public abstract class BaseThemeActivity extends BaseActivity {
    private static final String URL_MASTER_PASSWORD;
    private static final String URL_OTA = Util.API_HOST + "/api/jxwota/apk/checkBySn?apkIsbn=zndzgkptv5";
    PlatSignatureWrapper mPlatformSignature;
    private BroadcastReceiver networkReceiver = new AnonymousClass1();

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.jxw.launcher.BaseActivity, android.support.v4.app.FragmentActivity, android.support.v4.app.SupportActivity, android.app.Activity
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        if (Build.VERSION.SDK_INT >= 21) {
            Window window = getWindow();
            window.clearFlags(201326592);
            window.getDecorView().setSystemUiVisibility(1792);
            window.addFlags(Integer.MIN_VALUE);
            window.setStatusBarColor(0);
            window.setNavigationBarColor(0);
        }
        createDefDirs();
        this.mPlatformSignature = new PlatSignatureWrapper(this);
        getClass().getSimpleName().equals("ThemeActivity");
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // android.support.v4.app.FragmentActivity, android.app.Activity
    public void onDestroy() {
        super.onDestroy();
        getClass().getSimpleName().equals("ThemeActivity");
    }

    @Override // com.jxw.launcher.BaseActivity, android.support.v4.app.FragmentActivity, android.app.Activity
    public void onResume() {
        super.onResume();
        getWindow().getDecorView().setSystemUiVisibility(4866);
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // android.support.v4.app.FragmentActivity, android.app.Activity
    public void onPause() {
        super.onPause();
    }

    @Override // com.jxw.launcher.BaseActivity, android.app.Activity, android.view.Window.Callback
    public void onWindowFocusChanged(boolean z) {
        super.onWindowFocusChanged(z);
        if (z) {
            StudyRecordMng.stopCount(this);
        }
    }

    private void createDefDirs() {
        String[] stringArray = getResources().getStringArray(R.array.default_directories);
        if (stringArray == null) {
            return;
        }
        String absolutePath = Environment.getExternalStorageDirectory().getAbsolutePath();
        for (String str : stringArray) {
            File file = new File(absolutePath + InternalZipConstants.ZIP_FILE_SEPARATOR + str);
            if (!file.exists()) {
                file.mkdirs();
            }
            file.setReadable(true, false);
        }
    }

    /* renamed from: com.jxw.launcher.BaseThemeActivity$1  reason: invalid class name */
    /* loaded from: classes.dex */
    class AnonymousClass1 extends BroadcastReceiver {
        AnonymousClass1() {
        }

        @Override // android.content.BroadcastReceiver
        public void onReceive(Context context, Intent intent) {
            NetworkInfo activeNetworkInfo;
            if (ReceiverUtil.ACTION_CONNECTIVITY_ACTION.equals(intent.getAction()) && (activeNetworkInfo = ((ConnectivityManager) context.getSystemService("connectivity")).getActiveNetworkInfo()) != null && activeNetworkInfo.isConnected()) {
                BaseThemeActivity.this.mPlatformSignature.start(new PlatSignatureWrapper.OnVerify() { // from class: com.jxw.launcher.BaseThemeActivity.1.1
                    @Override // com.jht.engine.platsign.PlatSignatureWrapper.OnVerify
                    public void onError(boolean z) {
                        if (z) {
                            new Thread(new Runnable() { // from class: com.jxw.launcher.BaseThemeActivity.1.1.1
                                @Override // java.lang.Runnable
                                public void run() {
                                    SharedPreferences sharedPreferences = BaseThemeActivity.this.getSharedPreferences(MainActivity.PREFERENCE, 0);
                                    String string = sharedPreferences.getString(MainActivity.CUSTOMISBN, null);
                                    String string2 = sharedPreferences.getString("registFlag", null);
                                    String string3 = sharedPreferences.getString("series", null);
                                    String string4 = sharedPreferences.getString(MainActivity.SERIS, null);
                                    BaseThemeActivity.this.register(Util.API_HOST, sharedPreferences.getString("registMac", null), string, string2, string3, string4);
                                }
                            }).start();
                        }
                    }
                });
            }
        }
    }

    static {
        StringBuilder sb = new StringBuilder();
        sb.append(Util.API_HOST);
        sb.append("/api/jxwdevice/getconfig");
        URL_MASTER_PASSWORD = sb.toString();
    }

    private void basecheckUpdate() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request.Builder builder = new Request.Builder();
        okHttpClient.newCall(builder.url(URL_OTA + "&series=" + getDeviceSerial() + "&deviceType=OTHER").build()).enqueue(new Callback() { // from class: com.jxw.launcher.BaseThemeActivity.2
            @Override // okhttp3.Callback
            public void onFailure(Call call, IOException iOException) {
                Log.d("zzj", "onFailure=" + iOException.getMessage());
            }

            @Override // okhttp3.Callback
            public void onResponse(Call call, Response response) throws IOException {
                BaseThemeActivity.this.getOtaData(response);
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public synchronized void getOtaData(Response response) throws IOException {
        Log.d("zzj", "onResponse=" + response);
        if (response.isSuccessful()) {
            String string = response.body().string();
            PackageManager packageManager = MyApp.getInstance().getPackageManager();
            Log.d("zzj", "获取数据成功了:" + string);
            try {
                JSONArray jSONArray = new JSONObject(string).getJSONArray("data");
                final int i = 0;
                for (int i2 = 0; i2 < jSONArray.length(); i2++) {
                    JSONObject optJSONObject = jSONArray.optJSONObject(i2);
                    try {
                        if (optJSONObject.optInt("versionCode") > packageManager.getPackageInfo(optJSONObject.optString("packageName"), 0).versionCode) {
                            i++;
                            Log.e("num:", i + "");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                Log.e("finalnum:", i + "");
                runOnUiThread(new Runnable() { // from class: com.jxw.launcher.BaseThemeActivity.3
                    @Override // java.lang.Runnable
                    public void run() {
                        if (i > 0) {
                            BaseThemeActivity baseThemeActivity = BaseThemeActivity.this;
                            new DialogUpdate(baseThemeActivity, "" + i).show();
                            return;
                        }
                        Log.e("jjjj", "333333333");
                    }
                });
            } catch (JSONException e2) {
                e2.printStackTrace();
            }
        }
    }

    public void getMasterPassword() {
        try {
            if (TextUtils.isEmpty(SPUtils.getToken())) {
                return;
            }
            new OkHttpClient().newCall(new Request.Builder().addHeader("token", SPUtils.getToken()).url(URL_MASTER_PASSWORD).build()).enqueue(new Callback() { // from class: com.jxw.launcher.BaseThemeActivity.4
                @Override // okhttp3.Callback
                public void onFailure(Call call, IOException iOException) {
                    Log.d("zzj", "onFailure=" + iOException.getMessage());
                }

                @Override // okhttp3.Callback
                public void onResponse(Call call, Response response) throws IOException {
                    try {
                        String string = response.body().string();
                        UserData.setMasterPassword(new JSONObject(new JSONObject(string).optString("data")).optString("allpasswd"));
                        Log.d("zzj", "response=" + string);
                    } catch (Exception unused) {
                    }
                }
            });
        } catch (Exception unused) {
        }
    }

    public String getDeviceSerial() {
        String str = SystemPropertiesProxy.get(this, "ro.serialno");
        if (TextUtils.isEmpty(str)) {
            if (Build.VERSION.SDK_INT >= 29) {
                str = Build.getSerial();
            } else {
                str = Build.SERIAL;
            }
        }
        return (TextUtils.isEmpty(str) || str.length() < 8) ? SystemPropertiesProxy.get(this, "persist.sys.serialno") : str;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public String register(String str, String str2, String str3, String str4, String str5, String str6) {
        Logger.getLogger().e("核实注册" + str2 + ",series=" + str5 + ",serial=" + str6 + ",apkV=" + str3);
        try {
            TreeMap treeMap = new TreeMap();
            treeMap.put("appIsbn", str4);
            treeMap.put("series", str5);
            treeMap.put(IjkMediaMeta.IJKM_KEY_TYPE, "default");
            String str7 = (((str + "/api/jxwregister/v2/checkseries?") + "appIsbn=" + str4) + "&series=" + str5) + "&type=default";
            SharedPreferences sharedPreferences = getSharedPreferences(MainActivity.PREFERENCE, 0);
            String str8 = MyApp.getInstance().checkseriesToken;
            if (!TextUtils.isEmpty(str8)) {
                treeMap.put("token", str8 + "");
                str7 = str7 + "&token=" + str8;
            }
            long currentTimeMillis = System.currentTimeMillis();
            treeMap.put("timestamp", currentTimeMillis + "");
            String str9 = str7 + "&timestamp=" + currentTimeMillis;
            String genrateKey = SignUtil.genrateKey();
            treeMap.put("nonce", genrateKey);
            treeMap.put("appsecret", Constant.SIGN_KEY);
            String str10 = (str9 + "&nonce=" + genrateKey) + "&signature=" + SignUtil.signature(treeMap);
            Logger.getLogger().e("核实注册 url=====:" + str10);
            String remoteRequest = getRemoteRequest(str10);
            Logger.getLogger().e("核实注册 getRemoteRequest, result==:" + remoteRequest);
            if (remoteRequest != null) {
                JSONObject jSONObject = new JSONObject(remoteRequest);
                if (jSONObject.getString("code").equals("0")) {
                    JSONObject jSONObject2 = new JSONObject(jSONObject.getString("data"));
                    if (!jSONObject2.getBoolean("tokenException") && !jSONObject2.getBoolean("disabled") && !jSONObject2.getBoolean("expired")) {
                        if (!jSONObject2.getBoolean("registed")) {
                            SharedPreferences.Editor edit = sharedPreferences.edit();
                            edit.putString("checkseriesToken", "");
                            edit.putString("series", "");
                            edit.putString(MainActivity.CUSTOMISBN, "");
                            edit.putString("token", "");
                            edit.apply();
                            MyApp.getInstance().checkseriesToken = "";
                        } else {
                            MyApp.getInstance().checkseriesToken = jSONObject2.getString("token");
                        }
                    }
                    Intent intent = new Intent(this, SnRepeatActivity.class);
                    intent.putExtra("exceptionMsg", jSONObject2.getString("exceptionMsg"));
                    startActivity(intent);
                    SharedPreferences.Editor edit2 = sharedPreferences.edit();
                    edit2.putString("series", "");
                    edit2.putString(MainActivity.CUSTOMISBN, "");
                    edit2.putString("token", "");
                    edit2.apply();
                    MyApp.getInstance().checkseriesToken = "";
                }
            }
            return remoteRequest;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private static String getRemoteRequest(String str) throws UnknownHostException {
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new DefaultHttpClient().execute(new HttpGet(str)).getEntity().getContent()));
            StringBuffer stringBuffer = new StringBuffer();
            while (true) {
                String readLine = bufferedReader.readLine();
                if (readLine != null) {
                    stringBuffer.append(readLine);
                } else {
                    bufferedReader.close();
                    return stringBuffer.toString();
                }
            }
        } catch (UnknownHostException e) {
            throw e;
        } catch (Exception e2) {
            e2.printStackTrace();
            return null;
        }
    }

    public static String signature(SortedMap<String, String> sortedMap) {
        StringBuilder sb = new StringBuilder();
        for (String str : sortedMap.keySet()) {
            sb.append(str);
            sb.append("=");
            sb.append(sortedMap.get(str));
            sb.append("&");
        }
        sb.setLength(sb.length() - 1);
        return encryptSHA1(sb.toString());
    }

    public static String encryptSHA1(String str) {
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
            messageDigest.update(str.getBytes());
            byte[] digest = messageDigest.digest();
            StringBuffer stringBuffer = new StringBuffer();
            for (byte b : digest) {
                String hexString = Integer.toHexString(b & 255);
                if (hexString.length() < 2) {
                    stringBuffer.append(0);
                }
                stringBuffer.append(hexString);
            }
            return stringBuffer.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }
}
